/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.groupb.circus.Databases;

import com.groupb.circus.interfaces.XmlDbDataSource;
import java.io.IOException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.xmldb.api.DatabaseManager;
import org.xmldb.api.base.Collection;
import org.xmldb.api.base.Database;
import org.xmldb.api.base.XMLDBException;


/**
 *
 * @author a
 */
public class XmlDbDataSourceIm implements XmlDbDataSource {

    private static XmlDbDataSourceIm dataS;
    private static String driver;
    private static String url;
    private static String user;
    private static String pass;
    
   
    

    /*
     * Singleton Pattern
     */
    private XmlDbDataSourceIm() {
    }

    public static XmlDbDataSourceIm getInstance(){

        if (dataS == null) {
            try {
               
                    dataS = new XmlDbDataSourceIm();
                    Properties pr = new Properties();

                    
                        
                        pr.load(XmlDbDataSourceIm.class.getClassLoader().getResourceAsStream("conquery.properties"));
                        driver = pr.getProperty("XmlDbDataSource/driverClassName");
                        url = pr.getProperty("XmlDbDataSource/url");
                        user = pr.getProperty("XmlDbDatasource/user");
                        pass = pr.getProperty("XmlDbDatasource/password");

                
                
                  
                
                          Class c = Class.forName(driver);
                            
                          Database db = (Database) c.newInstance();
                            
                          DatabaseManager.registerDatabase(db);
                           
            
            } catch (XMLDBException ex) {
                Logger.getLogger(XmlDbDataSourceIm.class.getName()).log(Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                Logger.getLogger(XmlDbDataSourceIm.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                Logger.getLogger(XmlDbDataSourceIm.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(XmlDbDataSourceIm.class.getName()).log(Level.SEVERE, null, ex);
            }catch (IOException ex) {
                Logger.getLogger(XmlDbDataSourceIm.class.getName()).log(Level.SEVERE, null, ex);
            }
        
        
            
      }//cierra if
        return dataS;
   }
        
    
    
    Collection col;
    
    @Override
    public Collection getCollection() throws XMLDBException {
       String aUserName = user;
       String aPassword = pass;
       col = getCollection(aUserName, aPassword);
        return col;
       
      /*  System.out.println("collection");
        return DatabaseManager.getCollection(url);*/
    }

    @Override
    public Collection getCollection(String aUserName, String aPassword) throws XMLDBException {
       
        col = (Collection) DatabaseManager.getCollection(url, aUserName, aPassword);
        return col;
    }

}
